package com.tedu;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.util.Random;

public class MyProducer {

    @Test
    public void send() throws Exception {
        //1.连接nameserver
        //1.1 创建生产者
        DefaultMQProducer producer = new DefaultMQProducer();
        //1.2 配置nameserver的连接地址
        producer.setNamesrvAddr("localhost:9876");
        //1.3 指定生产者组
        producer.setProducerGroup("test-producer01");
        //1.4 启动生产者
        producer.start();

        //2.1创建消息对象
        Message message=new Message();
        int num = new Random().nextInt(1000);
        String msgTxt=num+"号老闫金牌技师，为您提供服务";
        //2.2 填充消息内容
        message.setBody(msgTxt.getBytes(StandardCharsets.UTF_8));
        //2.3 提供主题名称
        message.setTopic("test-01-topic");
        //2.4 设置key
        message.setKeys("workerId");
        //2.5 设置tag
        message.setTags("靓女");
        //3.将消息发送
        //3.1 调用send方法
        SendResult send = producer.send(message);
        System.out.println("send.getSendStatus() = " + send.getSendStatus());
        while (true);
    }
}
